//
//  CZStatusCell.m
//  XinLang_OC
//
//  Created by WONG on 16/3/11.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "CZStatusCell.h"
#import "CZStatusTopView.h"
#import "CZStatusBottomView.h"
#import "CZStatus.h"
#import "CZPictureView.h"

@interface CZStatusCell ()




@end

@implementation CZStatusCell

- (void)setStatus:(CZStatus *)status {
    _status = status;
    
    self.topView.status = _status;
    
//    self.contentLabel.text = _status.text;
    self.contentLabel.attributedText = _status.attrText;

    //NSLog(@"count = %zd",_status.pic_urls.count);
    self.pictureView.status = _status;
    self.size = [self.pictureView calcViewSize];
    //NSLog(@"self.size = %f",self.size.width);
    /*
        更新约束
     */
//    [self.pictureView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(self.size);
//        make.left.equalTo(self).offset(8);
//        make.top.equalTo(self.contentLabel.mas_bottom).offset(8);
//    }];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI {
    /*
        添加子控件
    */
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.pictureView];
    [self.contentView addSubview:self.bottomView];
    
    /*
        添加约束
     */
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(ScreenWidth, 54));
        make.left.equalTo(self);
        make.top.equalTo(self);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(8);
        make.left.equalTo(self.mas_left).offset(8);
        make.right.equalTo(self);
    }];
    
//    // 拿到配图数据，计算size，重新更新约束
//    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(self.size);
//        make.left.equalTo(self).offset(8);
//        make.top.equalTo(self.contentLabel.mas_bottom).offset(8);
//    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(ScreenWidth, 44));
        make.left.equalTo(self);
        make.top.equalTo(self.pictureView.mas_bottom).offset(8);
//        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    
    /// cell的高度根据子控件来确定,需要约束顶部和底部
    /// 约束cell的底部和bottomView的底部重合
//    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
}

- (CGFloat)rowHeight:(CZStatus *)status {
    // 根据模型来设置内容, 当我们把传入进来的模型赋值给self.status cell里面的内容被设置成为status的内容
    self.status = status;
    // 根据新的内容更新约束
    [self layoutIfNeeded];
    // 获取底部视图的最大y值（cell的高度）
    return CGRectGetMaxY(self.bottomView.frame);
}

#pragma mark - lazy load

- (CZStatusTopView *)topView {
    if (!_topView) {
        _topView = [[CZStatusTopView alloc] init];
    }
    return _topView;
}
- (CZStatusBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[CZStatusBottomView alloc] init];
    }
    return _bottomView;
}
- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = [UIColor darkGrayColor];
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}
- (CZPictureView *)pictureView {
    if (!_pictureView) {
        _pictureView = [[CZPictureView alloc] init];
    }
    return _pictureView;
}

@end



